package com.example.onedemotest.component;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public class CustomComponent extends Component implements Component.EstimateSizeListener, Component.DrawTask, Component.TouchEventListener {

    private static final String ATTR_RING_WIDTH = "ring_width";

    private static final String ATTR_RING_RADIUS = "ring_radius";

    private static final String ATTR_DEFAULT_COLOR = "default_color";

    private static final String ATTR_PRESSED_COLOR = "pressed_color";

    public float ringWidth = 20f; //圆环宽度

    public float ringRadius = 100f; //圆环半径

    public Color defaultColor = Color.YELLOW; //默认颜色

    public Color pressedColor = Color.CYAN; //按压态颜色

    private Paint circlePaint;

    public CustomComponent(Context context) {
        super(context);
    }

    //想在xml 中使用必须重写两个参数的方法
    public CustomComponent(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initAttrSet(attrSet);
        initPaint();
        addDrawTask(this);
        setTouchEventListener(this::onTouchEvent);
    }

    private void initAttrSet(AttrSet attrSet){
        if (attrSet==null)return;
        if (attrSet.getAttr(ATTR_RING_WIDTH).isPresent()){
            ringWidth=attrSet.getAttr(ATTR_RING_WIDTH).get().getDimensionValue();
        }
        if (attrSet.getAttr(ATTR_RING_RADIUS).isPresent()){
            ringRadius=attrSet.getAttr(ATTR_RING_RADIUS).get().getDimensionValue();
        }
        if (attrSet.getAttr(ATTR_DEFAULT_COLOR).isPresent()){
            defaultColor=attrSet.getAttr(ATTR_DEFAULT_COLOR).get().getColorValue();
        }
        if (attrSet.getAttr(ATTR_PRESSED_COLOR).isPresent()){
            pressedColor=attrSet.getAttr(ATTR_PRESSED_COLOR).get().getColorValue();
        }
    }

    private void initPaint(){
        circlePaint=new Paint();
        circlePaint.setColor(defaultColor);
        circlePaint.setStyle(Paint.Style.STROKE_STYLE);
        circlePaint.setStrokeWidth(ringWidth);
    }

    //测量宽高等
    @Override
    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
        //获取测量模式
        int widthSpec = EstimateSpec.getMode(widthEstimateConfig);
        int heightSpec = EstimateSpec.getMode(heightEstimateConfig);

        int widthSizeWithMode = 0;
        int heightSizeWithMode=0;
        switch (widthSpec){
            case EstimateSpec.UNCONSTRAINT:
                //没有限制
            case EstimateSpec.PRECISE: //父组件已确定子组件的大小。
                int width=EstimateSpec.getSize(widthSpec);
                 widthSizeWithMode = EstimateSpec.getSizeWithMode(width, widthSpec);
                break;
            case EstimateSpec.NOT_EXCEED://可用最大大小
                widthSizeWithMode = EstimateSpec.getSize(widthSpec);
        }

        switch (heightSpec){
            case EstimateSpec.UNCONSTRAINT:
                //没有限制
            case EstimateSpec.PRECISE: //父组件已确定子组件的大小。
                int width=EstimateSpec.getSize(widthSpec);
                heightSizeWithMode = EstimateSpec.getSizeWithMode(width, widthSpec);
                break;
            case EstimateSpec.NOT_EXCEED://可用最大大小
                heightSizeWithMode = EstimateSpec.getSize(widthSpec);
        }
        setEstimatedSize(widthSizeWithMode,heightSizeWithMode);
        return true;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.drawCircle(getWidth()/2,getHeight()/2,ringRadius,circlePaint);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()){
            case TouchEvent.PRIMARY_POINT_DOWN:
                //按下不松
                circlePaint.setColor(pressedColor);
                invalidate();
                new ToastDialog(getContext()).setText("按下了").show();
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                //抬起
                circlePaint.setColor(defaultColor);
                new ToastDialog(getContext()).setText("抬起了").show();
                invalidate();
            break;
        }
        return false;
    }
}
